/*测试数据*/
var testdata=[
	{
		id: 1,
		brandName: 'linda',
		createTime: 1502937009
	},
	{
		id: 1,
		brandName: 'dadd',
		createTime: 1502937009
	},
	{
		id: 1,
		brandName: 'ds',
		createTime: 1502937009
	},
	{
		id: 1,
		brandName: 'ssfdd',
		createTime: 1502937009
	}
];

$(function(){
	getTableData();
});
/*获取表格数据*/
function getTableData(){
	/*var backData=testdata;//[];
	tableInit(backData);*/
	$.post(
        gobalServer+"productBrand/selectAll",
        {},
        function(backData){
            if(backData.statusCode==200){
                var backData=backData.commonList;//[];
                tableInit(backData);
            }else{
                consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

            }
        }
	);
};
/*表格初始化*/
function tableInit(backData){
	var objectArray=[];
    if(backData.length>0){
        objectArray=dataToJson(backData);
    }
    $('#stockTable').bootstrapTable('destroy').bootstrapTable({
        toolbar: '#toolbar',                //工具按钮用哪个容器
        pageNumber:1,                       //初始化加载第一页，默认第一页
        pageSize: 20,                       //每页的记录行数（*）
        minimumCountColumns: 2,             //最少允许的列数
        clickToSelect: true,                //是否启用点击选中行
        height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
        data: objectArray,
        pageList: [5, 10, 20, 40, 100, 200]/*,
        rowStyle: function rowStyle(row,index) {
            if(row.mark!=null && row.mark>0){
                return {
                    css:{
                        "background-color": "#ffc930",
                        "color": "#fff"
                    }
                };
            }else{
                return {
                    css:{

                    }
                };
            }
        }*/
    });
};
/*表格数据封装*/
function dataToJson(dataResult) {
    var objectArray=new Array();
    $.each(dataResult,function(index,item){
    	var createTime="--";
    	if(item.createTime!=null){
        	createTime=formatDateTimeYMDHMS(item.createTime);
       }
        var operate="";
        operate="<a class='btn-a-common' href='"+gobalWeb+"product/brandmg.html?tgid="+item.id+"' >修改</a>";
        operate+="<a class='btn-a-common' href='javascript:;' onclick='deleteItemData("+item.id+")'>删除</a>";
        var arr={
        	"id":item.id,
        	"brandName":item.brandName,
            "createTime":createTime,
            "createTimesort":item.createTime,
            "operate":operate
        };
        objectArray.push(arr);
    });
    return objectArray;
};
/*删除数据*/
function deleteItemData(tgid){
	if(tgid!=null && tgid>0){
		var msg = "您真的确定要删除吗？\n\n删除后无法撤回！";
		if (confirm(msg)==true){
			consoleMes($("#manageContent"),2000,"success","数据已经成功删除！");
        	setTimeout(function () {
		    	window.location.reload();
		    }, 2500);
            $.post(
                gobalServer+"productBrand/delete",
                {"id":tgid},
                function(backData){
                    if(backData.statusCode==200){
                        consoleMes($("#manageContent"),2000,"success",backData.msg);
                        setTimeout(function () {
                            window.location.reload();
                        }, 2500);
                    }else{
                        consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

                    }
                }
            );

		}
	}else{
		consoleMes($("#manageContent"),2000,"warning","删除失败，请刷新页面重试！");//success、info、warning、danger
	}
};
